const app = getApp()
Page({
  data: {
    logList: [],
    scrollHeight: 0,
    currentDrawerData: {}
  },
  toPage(e) {
    app.navigateTo(e.currentTarget.dataset.link)
  },
  onLoad(option) {
    if (!option.id) {
      wx.showToast({
        title: '参数错误',
        icon: 'error'
      })
      wx.navigateBack()
    } else {
      this.setData({
        deviceID: option.id
      })
      this.getLogList()
    }

  },
  getLogList() {
    app.axios('device', {
      op: 'ajaxDevlogList2'
    }).then(res => {
      if (res.code && res.lists.length > 0) {
        this.setData({
          logList: this.reformData(res.lists)
        });

      }
    })
  },
  reformData(source) {
    const newLogList = source.map(item => {
      return {
        id: item.id,
        title: item.rname,  // 抽取 rname 作为 title
        list: (item.sxxlis || []).map(subItem => ({ // 截取前6条
          id: subItem.id,
          dateTime: app.dateFormatter(subItem.addtime, true), // 处理 addtime
          status: Number(subItem.status),
          statusText: Number(subItem.status) === 3 ? "上线" : "离线" // 状态文本转换
        }))
      };
    });
    return newLogList;
  },
  viewMore(e) {
    const targetId = e.currentTarget.dataset.id // 先提取 ID

    const infoItem = this.data.logList.find(item => item.id === targetId)
    this.setData({
      currentDrawerData: infoItem
    })
    const drawer = this.selectComponent('#v-drawer')
    drawer.toggleDrawer()
    console.log(infoItem)
  }
})